<?php
function buildTableLocation($urlRoot)
{
global $dbsql,$language;

    $sql_select = "select iId,cUrlString,cName from tbllocation where iParentId=0 
                    and cLanguage='$language' and iStatus=1 order by iOrder";
    $rs         = $dbsql->query($sql_select);
    
    $html       = '<table id="tblLocation" width="100%" cellpadding="0" cellspacing="0">';
    $html      .= '<tr>';
    $childhtml  = '';
    while($result   = $dbsql->fetch_array($rs))
    {
        $url    = '/'.$urlRoot.'/'.$result['cUrlString'];
        $childhtml  .= buildChildLocation($result['iId'],$url);
        $html  .= '<th><a href="'.$url.'" title="'.$result['cName'].'">'.$result['cName'].'</a></th>';
    }
    $html      .= '</tr>';
    $html      .= '<tr>'.$childhtml.'</tr>';
    $html      .= '</table>';
    return $html;
}

function buildChildLocation($idP,$urlP)
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cUrlString,cName from tbllocation where iParentId=".(int)$idP." 
                and cLanguage='$language' order by iOrder";
    $rs         = $dbsql->query($sql_select);
    $html       = '<td>';
    while($result   = $dbsql->fetch_array($rs))
    {
        $url    = $urlP.'/'.$result['cUrlString'];
        $html  .= '<div class="hh"><span title="'.$result['cName'].'">'.$result['cName'].'</span></div>';
        $html  .= '<div class="include">'.getLang("Destinations include").':</div>';
        $html  .= buildchild($result['iId'],$urlP);
    }
    $html      .= '</td>';
    return $html;
}

function buildchild($idP,$urlP)
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cUrlString,cName from tbllocation where iParentId=".(int)$idP." 
                    and cLanguage='$language' and iStatus=1 order by iOrder";
    $rs         = $dbsql->query($sql_select);
    $html       = '<div class="llink">';
    while($result   = $dbsql->fetch_array($rs))
    {
        $html  .= '<a href="'.$urlP.'/'.$result['cUrlString'].'" title="'.$result['cName'].'">'.$result['cName'].'</a>';
        
    }
    $html      .= '</div>';
    return $html;
}

function LoadLocationByUrl($url)
{
    global $dbsql,$language;
    
    $sql_select = "select * from tbllocation where cUrlString='$url' and cLanguage='$language'";
    return $dbsql->query_fetch($sql_select);
}

function ListPhotoLocation($id,$limit=0)
{
	global $language,$dbsql;

	$sql_select = "SELECT * FROM tbllocation_photo WHERE iParentId = '$id' order by iOrder ";
    if($limit>0)
    {
        $sql_select .= " limit $limit";
    }
    return $dbsql->load($sql_select);
}

function getPhotoLocation($id)
{
   global $language,$dbsql;

	$sql_select = "SELECT cImage FROM tbllocation_photo WHERE iParentId = '$id' order by iOrder limit 1";
    $result     = $dbsql->query_fetch($sql_select);
    return $result['cImage'];
}

function ListVideoLocation($id)
{
    global $language,$dbsql;

	$sql_select = "SELECT * FROM tbllocation_video WHERE iParentId = '$id' order by iOrder ";
    return $dbsql->load($sql_select);
}

function ListOtherDestinations($id)
{
    global $dbsql,$language;
    
    $id         = intval($id);
    $sql_select = "select lo1.* from tbllocation as  lo1 join tbllocation as lo2 On 
                    lo1.iParentId=lo2.iParentId and lo2.iId=$id and lo1.iId!=$id and lo1.iStatus=1
                    order by lo1.iOrder  limit 3";
    return $dbsql->load($sql_select);
}

function getLinkLocParent($id)
{
    global $dbsql,$language;
    $id         = intval($id);
    if($id==0)
    {
        return ;
    }
    $sql_select = "select cUrlString,iParentId from tbllocation where iId=$id";
    $result     = $dbsql->query_fetch($sql_select);
    $link       = '';
    if($result!=false)
    {
        $link.=getLinkLocParent($result['iParentId']);
        $link      .= '/'.$result['cUrlString'];
    }    
    return $link;
}

function buildreadmore($msg)
{
    global $idreadmore;
    
    if(strlen(strip_tags($msg))<10)
    {
        return;
    }
    $idreadmore     = intval($idreadmore)+1;
    $html   = '<div class="divmore" id="divmore'.$idreadmore.'">'.$msg.'</div>';
    $html  .= '<a href="#readmore" class="readmore2" onclick="showmore(this,\'divmore'.$idreadmore.'\',\''.getLang("Read less").'\',\''.getLang("Read more").'\')">';
    $html  .= getLang("Read more").'</a>';    
    return $html;
}

function linkreadmore($act)
{
    global $html;
    $url     = '/'.$html->urlRoot.'/popup/'.$act.'/'.$html->getContent('cUrlString');
    $return  = '<a class="readmore2" href="#rm" ';
    $return .= 'onclick="openpopup(\''.$url.'\',800,450)">'.getLang("Read more").'</a>';
    return $return;
}

function ListIdLocationChild($id)
{
    global $dbsql,$language;
    $id         = intval($id);
    $sql_select = "select iId from tbllocation where iParentId=$id";
    $rs         = $dbsql->query($sql_select);
    $listid     = array($id);
    while($result   = $dbsql->fetch_array($rs))
    {
        $listchild  = ListIdLocationChild($result['iId']);
        $listid     = array_merge($listid,$listchild);
    }
    return $listid;
}

function ListIdLocationParent($id)
{
    global $dbsql,$language;
    $id         = intval($id);
    $sql_select = "select iParentId from tbllocation where iId=$id";
    $rs         = $dbsql->query($sql_select);
    $listid     = array($id);
    while($result   = $dbsql->fetch_array($rs))
    {
        if($result['iParentId']==0)
        {
            $listparent = ListIdLocationChild($id);
        }else
        {
            $listparent = ListIdLocationParent($result['iParentId']);
        }
        
        $listid     = array_merge($listid,$listparent);
    }
    return $listid;
}

function LinkLocationRoot()
{
    global $dbsql,$language;
    
    $sql_select = "select cUrlString from tblcategory where cType='location' and cLanguage= '$language' limit 1";
    $result     = $dbsql->query_fetch($sql_select);
    return '/'.$result['cUrlString'];    
}

function LinkLocationById($id)
{
    global $dbsql,$language;
    $id         = intval($id);
    $sql_select = "select cUrlString,iParentId from tbllocation where iId=$id";
    $result     = $dbsql->query_fetch($sql_select);
    $link       = '';
    if($result)
    {
        $linkparent = LinkLocationById($result['iParentId']);
        if(!empty($linkparent))
        {
            $link   = $linkparent;
        }
        else
        {
            $link   = '/'.$result['cUrlString'];
        }        
    }
    return $link;
}

function ListLocationById($idloc)
{
    global $dbsql,$language;
    $idloc          = intval($idloc);
    
    $sql_select     = "select iId,cUrlString,cImage,cName,iParentId,cDescription from tbllocation where 
                        iId in ($idloc) and iStatus=1 and cLanguage= '$language'";
    $rs         = $dbsql->query($sql_select);
    $locations  = array();
    $linkroot   = LinkLocationRoot();
    while($result=$dbsql->fetch_array($rs))
    {
        $result['cUrlString'] = $linkroot.LinkLocationById($result['iParentId']).'/'.$result['cUrlString'];
        $result['images']     = ListPhotoLocation($result['iId'],2);
        $locations[] = $result;
    }
    return $locations;
}

function ListLocationByTourId($idtour)
{
    global $dbsql,$language;
    
    $sql_select     = "select lo.iId,lo.cUrlString,lo.cImage,lo.cName,lo.iParentId,lo.cDescription 
                    from tbllocation as lo JOIN tblsettour_location as sl ON
                     lo.iId=sl.iLocationId and sl.iTourId='$idtour' 
                     and lo.iStatus=1 and lo.cLanguage= '$language'";
                    
    $rs         = $dbsql->query($sql_select);
    $locations  = array();
    $linkroot   = LinkLocationRoot();
    while($result=$dbsql->fetch_array($rs))
    {
        $result['cUrlString'] = $linkroot.LinkLocationById($result['iParentId']).'/'.$result['cUrlString'];
        $result['images']     = ListPhotoLocation($result['iId'],2);
        $locations[] = $result;
    }
    return $locations;
}
function ListAllLocation($idP=0,$group=0)
{
    global $dbsql,$language;

    $sql_select = "select iId,cName,iStatus from tbllocation where iParentId=$idP and cLanguage='$language' order by iOrder";
    $rs         = $dbsql->query($sql_select);
    $return     = '';
    $endline    = "";  
    while($result   = $dbsql->fetch_array($rs))
    {
        if($idP==0 && $result['iStatus']==1)
        {
            $group   = $result['iId'];
            $return .= 'lctcb.add({id: '.$result['iId'].',name: "'.$result['cName'].'",g: '.$idP.'});'.$endline;            
        }
        else if($result['iStatus']==1)
        {
            $return .= 'lctcb.add({id: '.$result['iId'].',name: "'.$result['cName'].'",g: '.$group.'});'.$endline;
        }
        $return .= ListAllLocation($result['iId'],$group);
               
    }
    return $return;
}

function ListLocationByParentId($idP)
{
    global $dbsql,$language;
    
    $sql_select = "select * from tbllocation where iParentId=$idP and cLanguage='$language' and iStatus=1 order by iOrder ";
    $rs         = $dbsql->query($sql_select);
    $locations  = array();
    $linkroot   = LinkLocationRoot();
    while($result=$dbsql->fetch_array($rs))
    {
        $result['cUrlString'] = $linkroot.LinkLocationById($result['iParentId']).'/'.$result['cUrlString'];
        $result['images']     = ListPhotoLocation($result['iId'],2);
        $locations[] = $result;
    }
    return $locations;
}


function ListLocationOption()
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cName from tbllocation where iParentId =0 and cLanguage = '$language' and iStatus =1 order by cName";
    $rs         = $dbsql->query($sql_select);
    
    $options      = '';
    while($result = $dbsql->fetch_array($rs))
    {
        $options .= '<option value="'.$result['iId'].'">'.$result['cName'].'</option>';
    }
    return $options;
}


function ListLocation($idparent = 0)
{
    global $dbsql,$language;
    
    $sql_select = "select iId,cName from tbllocation 
                where  cLanguage = '$language' and iStatus =1 and iParentId = '$idparent'";
    if($idparent == 0)
    {
        $sql_select.= "  order by iOrder ";
    }else
    {
        $sql_select.= "  order by cName ";
    }
    return $dbsql->load($sql_select);
}
